<?php
/**
 * APNSBase
 * Basic Settings and Commands for Feedback and Push Service
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author  Sebastian Borggrewe <me@sebastianborggrewe.de>
 * @since   2010/01/14
 * @package APNP
 */

class APNSBase {

    /**
   * Defines wether Push Notification should be send
   * in test mode while developing or in production
   * mode when the app is released.
   * Possible values:
   * $_ENVIROMENT['development'] or $_ENVIROMENT['production']
   *
   * @var string
   */
  protected $_enviroment = 'development';

  /**
   * Path to the private key file
   *
   * @var string
   */
  protected $_privateKey = '';

  /**
   * Passphrase for the private key
   * If you set a passphrase when you generated
   * the private key you need to set it.
   * Otherwise leave the passphrase blank.
   *
   * @var string
   */
  protected $_privateKeyPassphrase = '';


   /**
   * Initializes a Push Notification with an
   * enviroment.
   * Valid values: development, production
   *
   * @param string $enviroment (production or developement)
   */
  public function __construct($enviroment)
  {
    if($enviroment !== 'development' && $enviroment !== 'production')
      throw new Exception('The Enviroment needs to be set to either development oder production');

    $this->_enviroment = $enviroment;
  }

  /**
   * Path to the private key file generated by
   * Apple
   *
   * @param string $privateKey
   */
  public function setPrivateKey($privateKey)
  {

    # Only accepts valid files
    if($this->_checkPrivateKey($privateKey) === false)
      throw new Exception('The Path to the private key is invalid.');

    $this->_privateKey = $privateKey;
  }

  /**
   * Passphrase for the specified private key
   *
   * @param string $privateKeyPassphrase
   */
  public function setPrivateKeyPassphrase($privateKeyPassphrase)
  {
    $this->_privateKeyPassphrase = $privateKeyPassphrase;
  }

  /**
   * Checks wether the private Key
   * is a valid file
   *
   * @param string $privateKey
   * @return bool
   */
  protected function _checkPrivateKey($privateKey)
  {
    if(trim($privateKey) !== '' && file_exists($privateKey))
      return true;
    else
      return false;
  }
}

?>